const express = require('express')
const { readFileSync } = require('fs')
const { resolve } = require('path')
const app = express()

app.all('*', function (req, res, next) { // 跨域
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'POST,GET')
  next()
})

app.get('/getTeacher', function (req, res) {
  const teacherData = JSON.parse(readFileSync(resolve(__dirname, './data/teacher.json', ), 'utf8'))
  res.send(teacherData)
})

app.get('/getStudents', function (req, res) {
  const studentsData = JSON.parse(readFileSync(resolve(__dirname, './data/students.json', ), 'utf8'))

  res.send(studentsData)
})

app.listen(8080, function () {
  console.log('Welcome to use Express!!!')
})